package com.kanche.bigbaby;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.kanche.bigbaby.activity.MoreActivity;
import com.kanche.bigbaby.activity.UserInfoActivity;
import com.kanche.bigbaby.collection.VehicleEditorStep1Activity;
import com.kanche.bigbaby.fragment.CollectionFragment;
import com.kanche.bigbaby.fragment.DazhixinFragment;
import com.kanche.bigbaby.fragment.StockFragment;
import com.kanche.bigbaby.support.base.BaseToolbarActivity;
import com.kanche.bigbaby.support.shapeIV.mask.PorterCircularImageView;
import com.kanche.bigbaby.support.utils.ViewUtility;

/**
 * Created by Chenshao_Young on 15/5/15.
 */
public class MainToolbarActivity extends BaseToolbarActivity implements View.OnClickListener{

    DrawerLayout drawerlayout;
    ActionBarDrawerToggle drawerToggle;
    PorterCircularImageView civ_avatar;

    LinearLayout rl_left_collect,rl_left_stock,rl_left_dazhixin;
    LinearLayout rl_left_more;
    String[] titles ;
    int [] leftMenuIds = {R.id.rl_left_collect,R.id.rl_left_stock,R.id.rl_left_dazhixin};
    int curChosePos = 0;
    Fragment collectionFragment,stockFragment,dazhixinFragment;
    @Override
    protected int getContentViewId() {
        return R.layout.activity_maintoolbar;
    }

    @Override
    protected void initViewAndListenerAndData() {
        initToolbar();

        drawerlayout = justFindIt(R.id.drawerlayout);
        drawerToggle = new ActionBarDrawerToggle(this,drawerlayout,toolbar,R.string.drawer_open,R.string.drawer_close);
        drawerToggle.syncState();
        drawerlayout.setDrawerListener(drawerToggle);


        titles = getResources().getStringArray(R.array.titles_drawer_tabs);

        civ_avatar = justFindIt(R.id.civ_avatar);
        civ_avatar.setOnClickListener(this);

        rl_left_more =  justFindIt(R.id.rl_left_more);
        rl_left_more.setOnClickListener(this);

        rl_left_collect =  justFindIt(R.id.rl_left_collect);
        rl_left_collect.setOnClickListener(this);
        rl_left_stock =  justFindIt(R.id.rl_left_stock);
        rl_left_stock.setOnClickListener(this);
        rl_left_dazhixin =  justFindIt(R.id.rl_left_dazhixin);
        rl_left_dazhixin.setOnClickListener(this);

//        leftChoseCurrent(curChosePos);
        rl_left_collect.performClick();

//        BigBabyApplication.instance.getBus().register();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_maintoolbar,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                Toast.makeText(MainToolbarActivity.this, "action_settings", Toast.LENGTH_SHORT).show();
//                Intent intent = new Intent(getApplicationContext(), SimpleActivity.class);
//                Intent intent = new Intent(getApplicationContext(), DropDownListViewDemo.class);
//                Intent intent = new Intent(getApplicationContext(), DifferentMenuActivity.class);
//                Intent intent = new Intent(getApplicationContext(), ListViewActivity.class);
//                startActivity(intent);
                break;
            case R.id.ab_collect:
                ViewUtility.intent_startActivityForResult(MainToolbarActivity.this,
                        VehicleEditorStep1Activity.class,AppConfig.MainToStep1);

                break;
            default:
                break;

        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_left_collect:{
                this.curChosePos = 0;
                if(null == collectionFragment){
                    collectionFragment = CollectionFragment.newInstance();
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.drawer_container,collectionFragment).commit();

                break;
            }
            case R.id.rl_left_stock:{
                this.curChosePos = 1;
                if(null == stockFragment){
                    stockFragment = StockFragment.newInstance();
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.drawer_container,stockFragment).commit();
                break;
            }
            case R.id.rl_left_dazhixin:{
                this.curChosePos = 2;
                if(null == dazhixinFragment){
                    dazhixinFragment = DazhixinFragment.newInstance();
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.drawer_container,dazhixinFragment).commit();

                break;
            }
            case R.id.civ_avatar:{
                Intent intent = new Intent(getApplicationContext(), UserInfoActivity.class);
                startActivity(intent);
                break;
            }
            case R.id.rl_left_more:{
                Intent intent = new Intent(getApplicationContext(), MoreActivity.class);
                startActivity(intent);
                break;
            }
        }

        leftChoseCurrent(curChosePos);

        drawerlayout.postDelayed(new Runnable() {
            @Override
            public void run() {

                drawerlayout.closeDrawers();
            }
        },200);




    }

    public void leftChoseCurrent(int curChosePos){
        for(int i=0;i<leftMenuIds.length;i++){
            if(i  == curChosePos){

                justFindIt(leftMenuIds[i]).setBackgroundColor(getResources().getColor(R.color.background_tab_pressed));
            } else {
                justFindIt(leftMenuIds[i]).setBackgroundColor(getResources().getColor(android.R.color.transparent));

            }
        }

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == AppConfig.MainToStep1 && resultCode == Activity.RESULT_OK){

            ((CollectionFragment)collectionFragment).onRefreshStarted(null);

        }
    }
}
